package Customer;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;
public class Admin extends User {
    private static Admin adminInstance = null;
    private Map<String, Customer> customers = new HashMap<>();


    // 搜索客户
    public void searchCustomer(String query) {
        boolean found = false;

        if (customers.containsKey(query)) {
            System.out.println(customers.get(query));
            found = true;
        } else {
            for (Customer customer : customers.values()) {
                if (customer.getUsername().equalsIgnoreCase(query)) {
                    System.out.println(customer);
                    found = true;
                    break;
                }
            }
        }

        if (!found) {
            System.out.println("未找到客户.");
        }
    }

    // 添加客户以进行测试的示例方法
    public void addCustomer(Customer customer) {
        customers.put(customer.getUsername(), customer);
    }

    private static final String DEFAULT_ADMIN_USERNAME = "admin";
    private static final String DEFAULT_ADMIN_PASSWORD = "ynuinfo#777";

    private Map<String, Product> products = new HashMap<>();

    private Admin(String username, String password) {
        super(username, password);
    }

    // 单例模式，确保只有一个管理实例
    public static Admin getInstance() {
        if (adminInstance == null) {
            adminInstance = new Admin(DEFAULT_ADMIN_USERNAME, DEFAULT_ADMIN_PASSWORD);
        }
        return adminInstance;
    }

    // 管理员登录方法
    public boolean login(String username, String password) {
        return this.getUsername().equals(username) && this.getPassword().equals(password);
    }

    // 更改管理员密码
    public void changePassword(String newPassword) {
        this.setPassword(newPassword);
        System.out.println("密码修改成功！");
    }

    // 重置客户密码
    public void resetCustomerPassword(String customerId, String newPassword) {
        if (customers.containsKey(customerId)) {
            Customer customer = customers.get(customerId);
            customer.setPassword(newPassword);
            System.out.println("用户密码修改成功！");
        } else {
            System.out.println("没有找到用户.");
        }
    }

    // 登出
    public void logout() {
        System.out.println("管理员登出！");
    }

    public void listAllCustomers() {
        if (customers.isEmpty()) {
            System.out.println("没有找到用户。");
            return;
        }

        for (Map.Entry<String, Customer> entry : customers.entrySet()) {
            System.out.println(entry.getValue());
        }
    }


    public void deleteCustomer(String customerId) {
        if (customers.containsKey(customerId)) {
            Scanner scanner = new Scanner(System.in);
            System.out.println("警告，你将删除id为: " + customerId+"的客户。");
            System.out.print("你确定你要删除吗（yes/no）: ");
            String confirmation = scanner.nextLine();

            if (confirmation.equalsIgnoreCase("yes")) {
                customers.remove(customerId);
                System.out.println("成功删除用户。");
            } else {
                System.out.println("客户删除取消。");
            }
        } else {
            System.out.println("没有找到客户。");
        }
    }


    public void findCustomerByIdOrUsername(String customerId, String username) {
        for (Customer customer : customers.values()) {
            if (customer.getCustomerId().equals(customerId) || customer.getUsername().equals(username)) {
                System.out.println(customer);
                return;
            }
        }
        System.out.println("没有找到用户。");
    }

    public void listAllProducts() {
        if (products.isEmpty()) {
            System.out.println("没有找到商品。");
            return;
        }

        for (Map.Entry<String, Product> entry : products.entrySet()) {
            Product product = entry.getValue();
            System.out.println("Product ID: " + product.getProductId());
            System.out.println("Product Name: " + product.getProductName());
            System.out.println("Manufacturer: " + product.getManufacturer());
            System.out.println("Production Date: " + product.getProductionDate());
            System.out.println("Model: " + product.getModel());
            System.out.println("Purchase Price: " + product.getPurchasePrice());
            System.out.println("Retail Price: " + product.getRetailPrice());
            System.out.println("Quantity: " + product.getQuantity());
            System.out.println("-----------------------------");
        }
    }


    public void addProduct(Product product) {
        if (products.containsKey(product.getProductId())) {
            System.out.println("产品已经存在。");
        } else {
            products.put(product.getProductId(), product);
            System.out.println("成功添加商品！");
        }
    }

    public void updateProduct(String productId, Product updatedProduct) {
        if (products.containsKey(productId)) {
            products.put(productId, updatedProduct);
            System.out.println("商品更新成功！");
        } else {
            System.out.println("没找到商品。");
        }
    }

    public void deleteProduct(String productId) {
        if (products.containsKey(productId)) {
            Scanner scanner = new Scanner(System.in);
            System.out.println("警告：你将要删除ID为： " + productId+"的产品");
            System.out.print("你确定你要继续吗(yes/no)? ");
            String confirmation = scanner.nextLine();

            if (confirmation.equalsIgnoreCase("yes")) {
                products.remove(productId);
                System.out.println("成功删除产品！");
            } else {
                System.out.println("操作取消！");
            }
        } else {
            System.out.println("未找到商品。");
        }
    }




    public List<Product> findProduct(String name, String manufacturer, double minPrice) {
        List<Product> result = new ArrayList<>();
        for (Product product : products.values()) {
            boolean matches = true;
            if (name != null && !product.getProductName().equalsIgnoreCase(name)) {
                matches = false;
            }
            if (manufacturer != null && !product.getManufacturer().equalsIgnoreCase(manufacturer)) {
                matches = false;
            }
            if (minPrice >= 0 && product.getRetailPrice() < minPrice) {
                matches = false;
            }
            if (matches) {
                result.add(product);
            }
        }

        if (result.isEmpty()) {
            System.out.println("未找到商品。");
        } else {
            for (Product product : result) {
                System.out.println(product);
            }
        }

        return result;
    }


}

